﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace PixelWarfare
{
    /// <summary>
    /// Screen to select all your blobs on before starting the game.
    /// </summary>
    class BlobSelectionScreen : Screen
    {
        #region Fields

        private static BlobSelectionScreen theOne;  // Singelton instance.

        private Game game;

        private bool hideUI;                        // Whether the bars and text are hidden.
        private bool invalidSelection;              // True when the user made an invalid selection.

        #endregion

        #region Constructors

        private BlobSelectionScreen()
        {
            game = Game.GetInstance();
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Gets the singleton instance of the class.
        /// </summary>
        /// <returns>Return the singleton instance.</returns>
        public static BlobSelectionScreen GetInstance()
        {
            if (theOne == null)
            {
                theOne = new BlobSelectionScreen();
            }

            /* Since were using it as a singleton we need to update it
             * as unexpired everytime we want to show it again. */
            theOne.ValidateScreen();

            return theOne;
        }

        /// <summary>
        /// Draws all the blob selections as radar circles,
        /// the outer bars to represent the amount of power
        /// each player has left and any notification text.
        /// 
        /// If the user is holding space dont draw the bars
        /// and some of the notification text.
        /// </summary>
        /// <param name="g">Graphics to be drawn on.</param>
        public override void Draw(Graphics g)
        {
            List<RadarCircle> selections = game.GetSelections();

            g.DrawImage(game.GetMap(), 0, 0);

            // Draw all the blob selections already made.
            foreach (RadarCircle rc in selections)
            {
                rc.Draw(g);
            }

            // If there is a blob selection currently being made, draw it.
            if (game.InSelection)
            {
                game.GetCurrentSelection().Draw(g);
            }

            // Draw bars and turn text if the ui isnt hidden.
            if (!hideUI)
            {
                for (int i = 0; i < Game.MaxPlayers; i++)
                {
                    Utility.DrawBar(g, i, game.GetPowerPercentage(i), game.GetColor(i));
                }

                if (!game.InSelection && game.MoreTurns() && !invalidSelection)
                {
                    Utility.DrawTextCentered(g, game.GetCurrentPlayerName() + "'S TURN", Color.White, -5);
                    Utility.DrawTextCentered(g, "(" + game.GetCurrentPlayerTurns() + ") LEFT", Color.White, 25, 0, 20);
                }
            }

            if (!game.MoreTurns())
            {
                Utility.DrawTextCentered(g, "CLICK TO START", Color.White);
            }
            else if (invalidSelection)
            {
                Utility.DrawTextCentered(g, "INVALID SELECTION", Color.Yellow);
            }

           
        }  

        /// <summary>
        /// Start a new blob selection if the location is valid.
        /// </summary>
        /// <param name="x">X coordinate the mouse was pressed at.</param>
        /// <param name="y">Y coordinate the mouse was pressed at.</param>
        public override void MousePress(int x, int y)
        {
            if (game.MoreTurns() && !game.NewSelection(x, y))
            {
                invalidSelection = true;
            }
            else if (!game.MoreTurns())
            {
                InvalidateScreen(GameScreen.GetInstance());
            }
        }

        /// <summary>
        /// End the new blob selection if the location is valid.
        /// </summary>
        /// <param name="x">X coordinate the mouse was released at.</param>
        /// <param name="y">Y coordinate the mouse was released at.</param>
        public override void MouseRelease(int x, int y)
        {
            game.EndSelection();
            invalidSelection = false;
        }

        /// <summary>
        /// Hide the bars and text while the space bar is pressed,
        /// and handle any other key events
        /// </summary>
        /// <param name="key">Key that was pressed.</param>
        public override void KeyPress(Keys key)
        {
            switch (key)
            {
                case Keys.Space:
                    hideUI = true;
                    break;

                case Keys.Escape:
                    InvalidateScreen(MainMenuScreen.GetInstance());
                    break;

                case Keys.Enter:
                    if (!game.MoreTurns())
                    {
                        InvalidateScreen(GameScreen.GetInstance());
                    }
                    break;
            }
        }

        /// <summary>
        /// Unhide the bars and text when space is released
        /// </summary>
        /// <param name="key">Key that was released.</param>
        public override void KeyRelease(Keys key)
        {
            switch (key)
            {
                case Keys.Space:
                    hideUI = false;
                    break;
            }
        }

        /// <summary>
        /// Nothing happens when you move the mouse.
        /// </summary>
        public override void MouseMove(int x, int y) { }

        /// <summary>
        /// Nothing needs to get updated on this screen.
        /// </summary>
        public override void Update() { }

        #endregion
    }
}
